<template>
  <div class="login">
    <el-card class="box-card">
       <template #header>
       <div class="card-header">
          <h1>教务后台管理系统</h1>
       </div>
      </template>
      <el-form
        :rules="rules"
        :model="form"
        ref="form"
        label-width="80px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="form.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="login('form')">登陆</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { nameRule, PassRule } from "@/utils/validate";
import { login } from "@/api/api";
import { setToken } from "@/utils/token";
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "approve123456",
      },
      rules: {
        username: [{ validator: nameRule }],
        password: [{ validator: PassRule }],
      },
    };
  },
  methods: {
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          console.log(this.form);
          login(this.form).then((res) => {
            console.log(res);
            if (res.data.status === 200) {
              let {username,token}=res.data
              setToken("username", username);
              setToken("token", token);
              this.$router.push("home");
            }
          });
        } else {
          console.log(this.form);
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
@import url("../assets/less/login.less");
</style>